package com.study.easyexcel.business.template;

import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import org.apache.poi.ss.usermodel.*;

import java.text.MessageFormat;
import java.util.HashMap;

/**
 * @author yuhuan
 * @date 2023/12/27
 */
public class DefaultFlatGroupStyle extends BaseFlatGroupStyleHandler {

    private DefaultFlatGroupStyle() {

    }

    public static DefaultFlatGroupStyle buildTitleRow(HashMap<Integer, String> titleRowColMap) {
        return build(titleRowColMap, null);
    }

    public static DefaultFlatGroupStyle buildTotalRow(HashMap<Integer, String> totalRowColMap) {
        return build(null, totalRowColMap);
    }

    public static DefaultFlatGroupStyle build(HashMap<Integer, String> titleRowColMap, HashMap<Integer, String> totalRowColMap) {
        DefaultFlatGroupStyle flatGroupStyleHandler = new DefaultFlatGroupStyle();
        flatGroupStyleHandler.titleRowColMap = titleRowColMap;
        flatGroupStyleHandler.totalRowColMap = totalRowColMap;
        return flatGroupStyleHandler;
    }

    @Override
    protected void setTitleCellStyle(CellWriteHandlerContext context, Cell cell) {
        int columnIndex = cell.getColumnIndex();
        Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
        CellStyle cellStyle = createCellUnifiedStyle(workbook, columnIndex);
        // 字体
        Font font = workbook.createFont();
        font.setFontName("宋体");
        font.setColor(IndexedColors.ROYAL_BLUE.getIndex());
        font.setBold(true);
        cellStyle.setFont(font);
        // 顶部边框
        cellStyle.setBorderTop(BorderStyle.THIN);
        // 水平居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        // 上下居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cell.setCellStyle(cellStyle);
    }

    @Override
    protected void setTotalCellStyle(CellWriteHandlerContext context, Cell cell) {
        int columnIndex = cell.getColumnIndex();
        Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
        CellStyle cellStyle = createCellUnifiedStyle(workbook, columnIndex);
        // 字体
        Font font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 10);
        font.setColor(IndexedColors.RED.getIndex());
        cellStyle.setFont(font);
        // 背景色
        cellStyle.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE1.getIndex());
        // 让背景色生效，不然无法显示背景颜色
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        // 底部边框
        cellStyle.setBorderBottom(BorderStyle.THIN);
        // 水平居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        // 上下居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 设置单元格公式
        int rowIndex = cell.getRowIndex();
        // 设置单元格格式
        DataFormat format = workbook.createDataFormat();
        // 百分比显示
        short percentage = format.getFormat("0.00%");
        // 保留两位小数
        short twoDecimal = format.getFormat("0.00");
        if (columnIndex == 3) {
            cellStyle.setDataFormat(twoDecimal);
            String fx = MessageFormat.format("IF(C{0}=0,0,IFERROR($I$1-C{0},\"\"))", rowIndex + 1);
            cell.setCellFormula(fx);
        }
        if (columnIndex == 6) {
            // 标题行到合计行中间数值的统计
            Integer titleRowIndex = currentTitleRowIndex(rowIndex);
            String fx = MessageFormat.format("SUM(G{0}:G{1})", titleRowIndex + 2, rowIndex);
            cell.setCellFormula(fx);
        }
        cell.setCellStyle(cellStyle);
    }
}
